package com.ea.admin.controller.superAdmin;

import com.ea.admin.permission.PermissionCode;
import com.ea.admin.pojo.Enum.Permission;
import com.ea.admin.pojo.param.superAdmin.database.BackupDatabaseParam;
import com.ea.admin.pojo.param.superAdmin.database.EditDatabaseUrlParam;
import com.ea.admin.pojo.param.superAdmin.database.GetDatabaseInfoParam;
import com.ea.admin.pojo.param.superAdmin.database.RecoverDatabaseParam;
import com.ea.admin.pojo.vo.superAdmin.GetDatabaseInfoVo;
import com.ea.admin.service.DatabaseService;
import com.ea.common.log.LogAnnotation;
import com.ea.common.result.Result;
import com.ea.common.utils.ParamUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.validation.Valid;

@RestController
@Api(tags = "超级管理-数据库管理")
@RequestMapping("/admin/super/database")
public class SuperDatabaseController {

    @Resource
    DatabaseService databaseService;

    @Transactional
    @Operation(summary = "数据管理页面")
    @PostMapping("/getDatabaseInfoVo")
    @ApiImplicitParam(dataType = "GetDatabaseInfoParam", name = "getDatabaseInfoParam", value = "分块下载文件", readOnly = true, required = true)
    @LogAnnotation(module = "DatabaseController", operation = "数据管理页面")
    @Permission(permission_code = PermissionCode.SUPER_MANAGE)
    public Result<GetDatabaseInfoVo> getDatabaseInfoVo(@RequestBody @Valid GetDatabaseInfoParam getDatabaseInfoParam) {
        GetDatabaseInfoVo result = databaseService.getDatabaseInfoVo(
                getDatabaseInfoParam.getPage(),
                getDatabaseInfoParam.getPageSize()
        );
        return Result.success(result);
    }

    @Transactional
    @ApiIgnore
    @Operation(summary = "修改数据库链接地址")
    @PostMapping("/editDatabaseUrl")
    @ApiImplicitParam(dataType = "EditDatabaseUrlParam", name = "editDatabaseUrlParam", value = "分块下载文件", readOnly = true, required = true)
    @LogAnnotation(module = "DatabaseController", operation = "修改数据库链接地址")
    @Permission(permission_code = PermissionCode.SUPER_EDIT_DATABASE_URL,
            content = "修改数据库链接地址为：{url}")
    public Result editDatabaseUrl(@RequestBody @Valid EditDatabaseUrlParam editDatabaseUrlParam) {
        databaseService.editDatabaseUrl(
                editDatabaseUrlParam.getUrl(),
                editDatabaseUrlParam.getUsername(),
                editDatabaseUrlParam.getPassword()
        );
        return Result.success(null);
    }

    @Transactional
    @Operation(summary = "恢复数据库备份")
    @PostMapping("/recoverDatabase")
    @ApiImplicitParam(dataType = "RecoverDatabaseParam", name = "recoverDatabaseParam", value = "分块下载文件", readOnly = true, required = true)
    @LogAnnotation(module = "DatabaseController", operation = "修改数据库链接地址")
    @Permission(permission_code = PermissionCode.SUPER_RECOVER_DATABASE,
            content = "恢复数据库备份为：{backupName}")
    public Result recoverDatabase(@RequestBody @Valid RecoverDatabaseParam recoverDatabaseParam) {
        databaseService.recoverDatabase(
                ParamUtil.parseId(recoverDatabaseParam.getMysqlBackupId())
        );
        return Result.success(null);
    }


    @Transactional
    @Operation(summary = "备份数据库")
    @PostMapping("/backupDatabase")
    @ApiImplicitParam(dataType = "BackupDatabaseParam", name = "backupDatabaseParam", value = "分块下载文件", readOnly = true, required = true)
    @LogAnnotation(module = "DatabaseController", operation = "备份数据库")
    @Permission(permission_code = PermissionCode.SUPER_RECOVER_DATABASE,
            content = "手动备份数据库，名为：{backupName}")
    public Result backupDatabase(@RequestBody @Valid BackupDatabaseParam backupDatabaseParam) {
        databaseService.backupDatabase(
                backupDatabaseParam.getBackupName()
        );
        return Result.success(null);
    }
}
